<?php
use oxide\plugin\PluginAbstract;
use oxide\http\Context;

\oxide\application\View::load('_html');

class FacebookPlugin extends PluginAbstract
{
   public function onInit(Context $context) 
   {
      
      $this->register('CommandRender');
      $this->register('ActionControllerViewRenderStart');
      $this->register('ModuleContentInfoRenderContentHeaderFooter');
      $this->register('ModuleContentViewContentRenderEnd');
   }
   
   public function onCommandRender($event, $sender, $args = null)
   {
      $config = $this->getConfig();
            
      $name_key = 'property';
      $type = 'blog';
      $site_name = $config->getValue('site_name');
      $admin_ids = $config->getValue('admin_ids');
      
      $insight_code = $config->getValue('insight');
      _html::meta('fb:admins', $admin_ids, $name_key);
      

   }
   
   public function onActionControllerViewRenderStart($event, $sender, $args = null)
   {
       // we should only use if this is content module and for view action
      if(!($sender->getModuleName() == 'content' &&
           $sender->getActionName() == 'view')) {
         return;
      }
      

      
      $root = <<<'EOD'
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
EOD;
      echo $root;
   }
   
   public function onModuleContentInfoRenderContentHeaderFooter($event, $sender, $args = null)
   {
       // we should only use if this is content module and for view action
      if(!(_url::module() == 'content' &&
           _url::action() == 'view')) {
         return;
      }
      
      $footers = $args['footers'];
      $content = $args['content'];
      $url = _content::url($content->permalink);
      $link = <<<EOF
<div class="fb-like" data-href="{$url}" data-send="false" data-layout="button_count" data-width="1" data-show-faces="false" data-font="arial"></div>
EOF;
      $footers[] = $link;
   }
   
   
   public function onModuleContentViewContentRenderEnd($event, $sender, $args = null)
   {
      $content = $args['content'];
      $url = _content::url($content->permalink);
      
      $config = $this->getConfig();
      $name_key = 'property';
      $type = 'blog';
      $site_name = $config->getValue('site_name');
      $admin_ids = $config->getValue('facebook_admin_ids');
      $image = null;
      
      /**
       * facebook/open social graph related meta data
       */
      _html::meta('og:type', $type, $name_key);
      _html::meta('og:site_name', $site_name, $name_key);
      _html::meta('og:admins', $admin_ids, $name_key);
      _html::meta('og:title', $content->title, $name_key);
      _html::meta('og:image', $image, $name_key);
      _html::meta('og:url', $url, $name_key);

      $code = <<<EOF
<section class='plugin-facebook'>
    <h3>Comments</h3>
<div class="fb-comments" data-href="{$url}" data-width="700" data-num-posts="10"></div>
</section>
EOF;
      echo $code;
      
   }
}



?>